package com.bdqn.controller;

import com.alibaba.fastjson.JSON;
import com.bdqn.common.Page;
import com.bdqn.pojo.Floor;
import com.bdqn.service.FloorService;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/admin/floor")
public class FloorController {

    @Resource
    private FloorService floorService;

    /**
     * 楼层管理页面
     * @param model
     * @return
     */
    @RequestMapping(value = "list",method = RequestMethod.GET)
    public ModelAndView list(ModelAndView model){
        model.setViewName("floor/list");
        return model;
    }

    /**
     * 分頁查詢楼层信息
     * @param name
     * @param page
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public String getList(@RequestParam(name = "name",required = false)String name, Page page){
        Map<String,Object> map = new HashMap<>();
        Map<String,Object> ret = new HashMap<>();
        map.put("name",name);
        map.put("offset",page.getOffset());
        map.put("pageSize",page.getRows());
        ret.put("rows",floorService.getList(map));
        ret.put("total",floorService.getTotal(map));
        return JSON.toJSONString(ret);
    }

    /**
     * 添加楼层信息
     * @param floor
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Map<String,String> add(Floor floor){
        Map<String,String> map = new HashMap<>();
        if (floor == null){
            map.put("type","error");
            map.put("msg","请填写正确的楼层信息");
            return map;
        }
        if(StringUtils.isEmpty(floor.getName())){
            map.put("type","error");
            map.put("msg","楼层名称不能为空");
            return map;
        }
        if (floorService.add(floor) <=0){
            map.put("type","error");
            map.put("msg","添加失败，请联系管理员");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "添加成功!");
        return map;
    }

    /**
     * 修改楼层信息
     * @param floor
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Map<String,String> edit(Floor floor){
        Map<String,String> map = new HashMap<>();
        if (floor == null){
            map.put("type","error");
            map.put("msg","请填写正确的楼层信息");
            return map;
        }
        if(StringUtils.isEmpty(floor.getName())){
            map.put("type","error");
            map.put("msg","楼层名称不能为空");
            return map;
        }
        if (floorService.edit(floor) <= 0){
            map.put("type","error");
            map.put("msg","修改失败");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "修改成功!");
        return map;
    }

    /**
     * 删除楼层信息
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public Map<String,String> delete(Long id){
        Map<String,String> map = new HashMap<>();
        if(id == null){
            map.put("type","error");
            map.put("msg","请选择要删除的信息");
            return map;
        }
        try {
            if (floorService.delete(id) <= 0){
                map.put("type","error");
                map.put("msg","删除失败");
                return map;
            }
        }catch (Exception e){
            map.put("type","error");
            map.put("msg", "该楼层下存在房间信息，请先删除该楼层下的所有房间信息!");
            return map;
        }
        map.put("type", "success");
        map.put("msg", "删除成功!");
        return map;
    }
}
